using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Animation_Editor
{
    public partial class PreviewControl : Control
    {
        int currentFrameNumber;
        List<Frame> frames;
        bool play;
        Frame currentFrame;


        public bool Play
        {
            get { return play; }
            set { play = value; }
        }

        public List<Frame> Frames
        {
            get { return frames; }
            set { frames = value; }
        }


        public int CurrentFrame
        {
            get { return currentFrameNumber; }
            set { currentFrameNumber = value; }
        }

        public PreviewControl()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            InitializeComponent();
            currentFrameNumber = 0;

            frames = new List<Frame>();
            currentFrame = new Frame();

            
        }

        Bitmap spriteSheet = null;
        public Bitmap SpriteSheet
        {
            get { return spriteSheet; }
            set
            {
                spriteSheet = value;
                Invalidate();
            }
        }


        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: Add custom paint code here
            if (spriteSheet != null)
            {
                pe.Graphics.DrawImage(spriteSheet, (this.Width / 2) + (currentFrame.RFrameRect.X -  currentFrame.PtAnchorPoint.X) , (this.Height / 2) + (currentFrame.RFrameRect.Y - currentFrame.PtAnchorPoint.Y), currentFrame.RFrameRect,System.Drawing.GraphicsUnit.Pixel);
            }
            // Calling the base class OnPaint
            base.OnPaint(pe);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateFrame();
        }

        private void UpdateFrame()
        {
            if (play == true)
            {
                if (frames.Count < 1)
                {
                    return;
                }
                currentFrameNumber++;

                if (currentFrameNumber > frames.Count - 1)
                {
                    currentFrameNumber = 0;
                }

                currentFrame = frames[currentFrameNumber];

                Invalidate();
                
            }


            
        }
    }
}
